package com.rslai.commons.validator.jsonvalidator.validators;

import org.junit.Assert;

import java.util.List;

import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.notNullValue;

/**
 * JSON校验器，判断字符串包含校验器
 */
public class ContainsStringValidator extends Validator {
    public static final String VALIDATOR_NAME = "containsStr";
    String containsStr;

    /**
     * 构造函数
     * @param params params
     */
    public ContainsStringValidator(List<Object> params) {
        if (params != null && params.size() > 0) {
            this.containsStr = String.valueOf(params.get(0));
        }
    }

    /**
     * 校验JSON对象
     * @param object 待验证的JSON对象
     */
    @Override
    public void doValidate(Object object) {
        Assert.assertThat(getCurrentKeyPath(), object, notNullValue());
        Assert.assertThat(getCurrentKeyPath(), String.valueOf(object), containsString(containsStr));
    }

    /**
     * 返回校验名称
     * @return 校验名称
     */
    @Override
    public String name() {
        return VALIDATOR_NAME;
    }

}
